/*
Copyright Nicolas Dessart 2010 Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

#ifndef SEQUENCE_HPP
#define SEQUENCE_HPP

#include <string>
#include <deque>

template <typename T>
class Sequence
{
public:
    Sequence() {}
    bool Get(size_t i, T& val)
    {
        if(m_values.size() > i)
        {
            val = m_values[i];
            return m_defines[i];
        }
        else
        {
            return false;
        }
    }

    bool Get(T& val)
    {
        if(!m_values.empty() && !(m_values.size() > 1))
        {
            val = m_values.back(); return m_defines.back();
        }
        else
        {
            return false;
        }
    }

    bool Set(size_t i, const T& val)
    {
        bool ret = i>=m_values.size();
        if(ret)
        {
            m_values.resize(i+1,T());
            m_defines.resize(i+1,false);
        }
        m_values[i] = val;
        m_defines[i] = true;

        return ret;
    }

    bool Set(const T& val)
    {
        bool ret = m_values.empty() || m_values.size() > 1;
        m_values.resize(1,val);
        m_defines.resize(1,true);
        return ret;
    }

    void Clear() {m_values.clear(); m_defines.clear();}

    size_t Size() {return m_values.size();}
private:
    std::deque<T> m_values;
    std::deque<bool> m_defines;


};

#endif // SEQUENCE_HPP
